package dync.rtmpc_demo.guest;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.anyrtc.rtmpc_hybrid.RTMPCGuestHelper;
import org.anyrtc.rtmpc_hybrid.RTMPCGuestKit;
import org.anyrtc.rtmpc_hybrid.RTMPCHybird;
import org.webrtc.RendererCommon;
import org.webrtc.VideoRenderer;

import butterknife.BindView;
import butterknife.OnClick;
import dync.rtmpc_demo.BaseActivity;
import dync.rtmpc_demo.R;
import dync.rtmpc_demo.utils.RTMPCVideoView;
import dync.rtmpc_demo.utils.ToastUtil;

public class GuestActivity extends BaseActivity implements RTMPCVideoView.BtnVideoCloseEvent, RTMPCGuestHelper {


    @BindView(R.id.rl_video_view)
    RelativeLayout rlVideoView;
    @BindView(R.id.tv_apply_line)
    TextView tvApplyLine;
    @BindView(R.id.tv_change_screen)
    TextView tvChangeScreen;
    private RTMPCGuestKit mGuestKit;
    private long mVideoRender;
    private RTMPCVideoView mVideoView;
    private boolean isApplyLine = false;
    private boolean isFullScreen = false;

    @Override
    public int getLayoutId() {
        return R.layout.activity_guest;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        String pullUrl = getIntent().getStringExtra("pull_url");
        String anyrtcid = getIntent().getStringExtra("anyrtcid");
        mVideoView = new RTMPCVideoView(rlVideoView, RTMPCHybird.Inst().Egl(), false);
        mVideoView.setBtnCloseEvent(this);
        RTMPCHybird.Inst().SetScreenToLandscape();
        mGuestKit = new RTMPCGuestKit(this, this);
        mVideoRender = mVideoView.OnRtcOpenLocalRender(RendererCommon.ScalingType.SCALE_ASPECT_FIT).GetRenderPointer();
        mGuestKit.StartRtmpPlay(pullUrl, mVideoRender);
        mGuestKit.JoinRTCLine(anyrtcid, "8888", "nothing");
    }

    @Override
    public void CloseVideoRender(View view, String s) {
        tvApplyLine.setText("连麦");
        if (mGuestKit != null) {
            mGuestKit.HangupRTCLine();
            isApplyLine = false;
        }
        if (mVideoView != null) {
            mVideoView.OnRtcRemoveRemoteRender("LocalCameraRender");
        }
    }

    @Override
    public void OnSwitchCamera(View view) {
        if (mGuestKit != null) {
            mGuestKit.SwitchCamera();
        }
    }

    @Override
    public void OnRtmplayerOK() {

    }

    @Override
    public void OnRtmplayerStart() {

    }

    @Override
    public void OnRtmplayerStatus(int i, int i1) {

    }

    @Override
    public void OnRtmplayerCache(int i) {

    }

    @Override
    public void OnRtmplayerClosed(int i) {

    }

    @Override
    public void OnRtmpAudioLevel(String s, int i) {

    }

    @Override
    public void OnRTCJoinLineResult(final int i, String s) {
        GuestActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (i == 0) {
                    ToastUtil.show("加入RTC服务成功");
                }
            }
        });
    }

    @Override
    public void OnRTCApplyLineResult(final int i) {
        GuestActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (i == 0) {
                    isApplyLine = true;
                    tvApplyLine.setText("挂断");
                    if (!isFullScreen){
                        screenChange();
                    }
                    VideoRenderer render = mVideoView.OnRtcOpenRemoteRender("LocalCameraRender", RendererCommon.ScalingType.SCALE_ASPECT_FIT);
                    mGuestKit.SetVideoCapturer(render.GetRenderPointer(), true);
                } else {
                    isApplyLine = false;
                    tvApplyLine.setText("连麦");
                    ToastUtil.show("主播拒绝连线");
                }
            }
        });
    }

    @Override
    public void OnRTCHangupLine() {
        GuestActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                isApplyLine = false;
                mGuestKit.HangupRTCLine();
                mVideoView.OnRtcRemoveRemoteRender("LocalCameraRender");
            }
        });
    }

    @Override
    public void OnRTCLineLeave(int i, String s) {
        GuestActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.show("活动结束");
                finishAnimActivity();
            }
        });
    }

    @Override
    public void OnRTCOpenVideoRender(final String s, String s1) {
        GuestActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final VideoRenderer render = mVideoView.OnRtcOpenRemoteRender(s, RendererCommon.ScalingType.SCALE_ASPECT_FIT);
                mGuestKit.SetRTCVideoRender(s, render.GetRenderPointer());
            }
        });
    }

    @Override
    public void OnRTCCloseVideoRender(final String s, String s1) {
        GuestActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mGuestKit.SetRTCVideoRender(s, 0);
                mVideoView.OnRtcRemoveRemoteRender(s);
            }
        });
    }

    @Override
    public void OnRTCOpenAudioLine(String s, String s1) {

    }

    @Override
    public void OnRTCCloseAudioLine(String s, String s1) {

    }

    @Override
    public void OnRTCLiveStart() {

    }

    @Override
    public void OnRTCLiveStop() {

    }

    @Override
    public void OnRTCUserMessage(String s, String s1, String s2, String s3) {

    }

    @Override
    public void OnRTCUserBarrage(String s, String s1, String s2, String s3) {

    }

    @Override
    public void OnRTCMemberListWillUpdate(int i) {

    }

    @Override
    public void OnRTCMember(String s, String s1) {

    }

    @Override
    public void OnRTCMemberListUpdateDone() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mGuestKit != null) {
            mGuestKit.StopRtmpPlay();
            mGuestKit.Clear();
            mVideoView.OnRtcRemoveLocalRender();
            mGuestKit = null;
        }
    }



    public void screenChange() {

        if (!isFullScreen) {//竖屏状态
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 横屏
        } else {//横屏状态
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 竖屏
//            mImmersionBar.statusBarColor(R.color.black).fitsSystemWindows(true).init();
        }
        isFullScreen = !isFullScreen;
    }


    @OnClick({R.id.tv_apply_line, R.id.tv_change_screen})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_apply_line:
                if (isApplyLine) {
                    if (mGuestKit != null) {
                        mGuestKit.HangupRTCLine();
                        tvApplyLine.setText("连麦");
                        isApplyLine = false;
                    }
                } else {
                    if (mGuestKit != null) {
                        mGuestKit.ApplyRTCLine("你好");
                        tvApplyLine.setText("挂断");
                        isApplyLine = true;
                    }
                }
                break;
            case R.id.tv_change_screen:
                if (isApplyLine){
                    ToastUtil.show("请先挂断连麦");
                    return;
                }
                screenChange();
                break;
        }
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        //   mActivityCreateRoom.setVisibility(View.VISIBLE);

        super.onConfigurationChanged(newConfig);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            isFullScreen = true;

        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            //  mVideoView.screenChange(false);
            isFullScreen = false;
        }

    }
}
